package com.uade.model;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Pojo
 * 
 * @author Alumno
 * 
 */
public class Usuario implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4001349087594714461L;

	private static Logger log = Logger.getLogger(Usuario.class);

	private long id;
	private String nombre;
	private String apellido;
	private String password;
	private String email;
	private String tipo;
	private List<Compra> compras;

	public Usuario() {
		super();
	}

	private Double coordenadaX;

	private Double coordenadaY;

	public Double getCoordenadaX() {
		return coordenadaX;
	}

	public void setCoordenadaX(Double coordenadaX) {
		this.coordenadaX = coordenadaX;
	}

	public Double getCoordenadaY() {
		return coordenadaY;
	}

	public void setCoordenadaY(Double coordenadaY) {
		this.coordenadaY = coordenadaY;
	}

	public Usuario(long id, String nombre, String apellido, String password,
			String email, String tipo, List<Compra> compras,
			Double coordenadaX, Double coordenadaY) {
		super();
		this.id = id;
		this.nombre = nombre;
		this.apellido = apellido;
		this.password = password;
		this.email = email;
		this.tipo = tipo;
		this.compras = compras;
		this.coordenadaX = coordenadaX;
		this.coordenadaY = coordenadaY;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getApellido() {
		return apellido;
	}

	public void setApellido(String apellido) {
		this.apellido = apellido;
	}

	public String getPassword() {
		return password;
	}

	/**
	 * Encripta el password de usuario en md5
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");

			md.update(password.getBytes());

			byte byteData[] = md.digest();

			// convert the byte to hex format method 1
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < byteData.length; i++) {
				sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
						.substring(1));
			}

			log.debug("Digest(in hex format):: " + sb.toString());

			// convert the byte to hex format method 2
			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < byteData.length; i++) {
				String hex = Integer.toHexString(0xff & byteData[i]);
				if (hex.length() == 1)
					hexString.append('0');
				hexString.append(hex);
			}
			log.debug("Digest(in hex format):: " + hexString.toString());
			this.password = hexString.toString();
		} catch (NoSuchAlgorithmException e) {

		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTipo() {
		return tipo;
	}

	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public List<Compra> getCompras() {
		return compras;
	}

	public void setCompras(List<Compra> compras) {
		this.compras = compras;
	}

}
